package com.cx.auth.service.impl;

import com.cx.auth.entity.ChatUser;
import com.cx.auth.mapper.ChatUserMapper;
import com.cx.auth.service.IChatUserService;
import com.ruoyi.common.core.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Validator;
import java.util.List;

/**
 * 用户 业务层处理
 *
 * @author ruoyi
 */
@Service
public class ChatUserServiceImpl implements IChatUserService
{
    private static final Logger logger = LoggerFactory.getLogger(ChatUserServiceImpl.class);

    @Autowired
    private ChatUserMapper chatUserMapper;

    @Autowired
    protected Validator validator;

    /**
     * 校验email是否唯一
     *
     * @param user 用户信息
     * @return
     */
    @Override
    public boolean checkEmailUnique(ChatUser user)
    {
        Long userId = StringUtils.isNull(user.getUserId()) ? -1L : user.getUserId();
        ChatUser chatUser = chatUserMapper.checkEmailUnique(user.getEmail());
        if (StringUtils.isNotNull(chatUser) && chatUser.getUserId().longValue() != userId.longValue())
        {
            return false;
        }
        return true;
    }

    /**
     * 新增保存用户信息
     *
     * @param chatUser 用户信息
     * @return 结果
     */
    @Override
    public int insertUser(ChatUser chatUser)
    {
        // 新增用户信息
        int rows = chatUserMapper.insertUser(chatUser);
        return rows;
    }

    /**
     * 注册用户信息
     *
     * @param chatUser 用户信息
     * @return 结果
     */
    @Override
    public boolean registerUser(ChatUser chatUser)
    {
        return chatUserMapper.insertUser(chatUser) > 0;
    }

    /**
     * 修改保存用户信息
     *
     * @param user 用户信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateUser(ChatUser user)
    {
        return chatUserMapper.updateUser(user);
    }


    @Override
    public ChatUser selectUserByEmail(String email)
    {
        return chatUserMapper.selectUserByEmail(email);
    }


    @Override
    public int updateUserProfile(ChatUser user)
    {
        return chatUserMapper.updateUser(user);
    }

    @Override
    public List<ChatUser> selectCommonUserList(ChatUser user) {
        return chatUserMapper.selectCommonUserList(user);
    }

    @Override
    public int updateUserStatus(ChatUser user) {
        return chatUserMapper.updateUser(user);
    }

    @Override
    public ChatUser selectUserById(String userId){
        return chatUserMapper.selectUserById(userId);
    }
}
